package com.buba.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import common.R;
import entity.salary.Insurance;
import entity.salary.dto.allInsucesDto;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.buba.service.InsuranceService;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author beisheng
 * @since 2023-03-14
 */
@RestController
@RequestMapping("/insurance")
public class InsuranceController {

    @Resource
    private InsuranceService insuranceService;

    /* *
     * @Author: beisheng
     * @Descriprion: 根据选择的险种查询险种详情
     * @Date: 2023/4/4 11:12
     * @Return: common.R
     * @Param: [checkedCities]
     */

    @PostMapping("/getInsurance")
    public R getInsurance(@RequestBody List<String> checkedCities){
        List<Insurance> insuranceList = insuranceService.getInsurance(checkedCities);
        return R.success(insuranceList);
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 查询全部社保或公积金险种
     * @Date: 2023/3/21 20:28
     * @Return: common.R
     * @Param: [insurType]
     */

    @GetMapping("/selSocialInsurance/{insurType}")
    public R selSocialInsurance(@PathVariable("insurType") Integer insurType){
        List<Insurance> list = insuranceService.list(new QueryWrapper<Insurance>().eq("delete_flag", 0).eq("insurance_type", insurType));
        return R.success(list);
    }
    /* *
     * @Author: beisheng
     * @Descriprion: 查询所有险种名称
     * @Date: 2023/3/16 16:19
     * @Return: common.R
     * @Param: []
     */

    @GetMapping("/selAllInsuName")
    public R selAllInsuName(@RequestParam Integer insuranceType){
        List<String> allInsuName = insuranceService.selAllInsuName(insuranceType);
        return R.success(allInsuName);
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 添加社保险种
     * @Date: 2023/3/21 20:14
     * @Return: common.R
     * @Param: [map]
     */

    @PostMapping("/saveSocialToInsurance")
    public R saveSocialToInsurance(@RequestBody Map<String,Object> map){
        insuranceService.saveSocialToInsurance(map);
        return R.success();
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 添加公积金险种
     * @Date: 2023/3/21 20:14
     * @Return: common.R
     * @Param: [map]
     */

    @PostMapping("/saveAccumToInsurance")
    public R saveAccumToInsurance(@RequestBody Map<String,Object> map){
        insuranceService.saveAccumToInsurance(map);
        return R.success();
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 修改险种状态
     * @Date: 2023/3/21 20:22
     * @Return: common.R
     * @Param: [insurId]
     */

    @GetMapping("/updateSocialStatus/{insurId}")
    public R updateSocialStatus(@PathVariable("insurId") Integer insurId){
        Insurance insurance = new Insurance();
        insurance.setInsuranceStatus(1);
        insurance.setUpdateTime(new Date());
        insuranceService.update(insurance,new QueryWrapper<Insurance>().eq("delete_flag",0).eq("sa_id",insurId));
        return R.success();
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 修改社保险种
     * @Date: 2023/3/21 20:14
     * @Return: common.R
     * @Param: [map]
     */

    @PostMapping("/updateSocialToInsurance")
    public R updateSocialToInsurance(@RequestBody Map<String,Object> map){
        insuranceService.updateSocialToInsurance(map);
        return R.success();
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 修改公积金险种
     * @Date: 2023/3/21 20:14
     * @Return: common.R
     * @Param: [map]
     */

    @PostMapping("/updateAccumToInsurance")
    public R updateAccumToInsurance(@RequestBody Map<String,Object> map){
        insuranceService.updateAccumToInsurance(map);
        return R.success();
    }

    /**
     * 功能描述: 新增或更新
     *
     * @param insurance
     * @return
     * @throws
     * @Author: zl
     */
    @PostMapping("/save")
    public R save(@RequestBody Insurance insurance) {
        insuranceService.saveOrUpdate(insurance);
        return R.success();
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 逻辑删除险种
     * @Date: 2023/3/21 20:29
     * @Return: common.R
     * @Param: [id]
     */

    @GetMapping("/delete/{id}")
    public R delete(@PathVariable Integer id) {
        Insurance insurance = new Insurance();
        insurance.setDeleteFlag(1);
        insuranceService.update(insurance,new QueryWrapper<Insurance>().eq("sa_id",id));
        return R.success();
    }

    /**
     * 功能描述: 批量删除
     *
     * @param ids
     * @return
     * @throws
     * @Author: zl
     */
    @PostMapping("/deleteBatch")
    public R deleteBatch(@RequestBody List<Integer> ids) {
        insuranceService.removeByIds(ids);
        return R.success();
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 根据名称查找险种
     * @Date: 2023/3/16 16:51
     * @Return: common.R
     * @Param: [allInsuName]
     */
    @PostMapping("/selAllInsuces")
    public R selAllInsuces(@RequestBody allInsucesDto allInsucesDto) {
        List<Insurance> insuranceList = insuranceService.selAllInsuces(allInsucesDto.getAllInsuName(),allInsucesDto.getInsuranceType());
        return R.success(insuranceList);
    }

    /**
     * 功能描述: 单查
     *
     * @param id
     * @return
     * @throws
     * @Author: zl
     */
    @GetMapping("/findOne/{id}")
    public R findOne(@PathVariable Integer id) {
        return R.success(insuranceService.getById(id));
    }

    /**
     * 功能描述: 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     * @throws
     * @Author: zl
     * @Date:   2022/7/28 19:15
     */
    @GetMapping("/findPage")
    public R findPage(@RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize) {
        QueryWrapper<Insurance> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return R.success(insuranceService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}
